package edu.mse.camel.client.session.actions;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.runtime.IAdaptable;
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.MessageBox;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.IEditorActionDelegate;
import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.IViewActionDelegate;
import org.eclipse.ui.IViewPart;

import edu.mse.camel.client.log.Debug;
import edu.mse.camel.client.session.plugin.Activator;
import edu.mse.camel.client.session.util.FilePublisherUtil;

public class TransferFileAction implements IViewActionDelegate, IEditorActionDelegate {
	private IFile [] selectedFiles;
	
	@Override
	public void init(IViewPart view) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void run(IAction action) {
		MessageBox messageBox = new MessageBox(new Shell(), SWT.OK);
		messageBox.setText("Publish these files");
		String msg = new String();
		for ( IFile file: selectedFiles) {
			msg = msg + file.getName() + "; ";
		}
		messageBox.setMessage(msg);
		messageBox.open();

		if(selectedFiles.length == 0) {
			Debug.log_info(Activator.PLUGIN_ID, "TransferFile : file array is empty");
		}
		FilePublisherUtil.publishFiles(Arrays.asList(selectedFiles));
	}

	@SuppressWarnings("unchecked")
	@Override
	public void selectionChanged(IAction action, ISelection selection) {
		ArrayList<IFile> newFiles = new ArrayList<IFile>();
		IFile file = null;
		if ( selection != null && selection instanceof IStructuredSelection ) {
			IStructuredSelection ss = (IStructuredSelection) selection;
			Iterator iter = ss.iterator();
			
			if(!iter.hasNext()) {
				Debug.log_info(Activator.PLUGIN_ID, "selectionChanged : selection has no next element");
			}
			
			for ( ; iter.hasNext();) {
				Object obj = iter.next();
				
				if(obj instanceof IFile) {
					Debug.log_info(Activator.PLUGIN_ID, "selectionChanged : got a file");
					file = (IFile) obj;
				}
				else if ( obj instanceof IAdaptable){
					Debug.log_info(Activator.PLUGIN_ID, "selectionChanged : got an IAdaptable");
					
					IAdaptable a = (IAdaptable) obj;
					IResource res = (IResource) a.getAdapter(IResource.class);
					if ( res instanceof IFile) {
						file = (IFile) res;
						Debug.log_info(Activator.PLUGIN_ID, "selectionChanged : got an IAdaptable and it can be converted to an IFile");
					}
					else {
						Debug.log_info(Activator.PLUGIN_ID, "selectionChanged : got an IAdaptable but cann't be converted to an IFile");
					}
				}
				
				if(file != null ) {
					// the eclipse book example has this query, but it seems no file is synced ??
					//if(!file.isSynchronized(IResource.DEPTH_ZERO)) {
						newFiles.add(file);
						Debug.log_info(Activator.PLUGIN_ID, "selectionChanged : add to newFiles");
					//}
				}
			}
			
		}
		else {
			Debug.log_info(Activator.PLUGIN_ID, "selectionChanged : null(selection) or not_instance_of(selection, IStructuredSelection)");
		}
		
		if ( newFiles.isEmpty()) {
			selectedFiles = null;
			action.setEnabled(false);
			Debug.log_info(Activator.PLUGIN_ID, "selectionChanged : newFile is empty");
		}
		else {
			selectedFiles = (IFile[]) newFiles.toArray(new IFile[newFiles.size()]);
			action.setEnabled(true);
			Debug.log_info(Activator.PLUGIN_ID, "selectionChanged : newFile is not empty");
		}
		
	}

	@Override
	public void setActiveEditor(IAction action, IEditorPart targetEditor) {
		// TODO Auto-generated method stub
		
	}

}
